package cn.bobohost.market.service.impl;

import cn.bobohost.market.dao.EmployeeMapper;
import cn.bobohost.market.pojo.Employee;
import cn.bobohost.market.service.EmployeeService;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 员工管理的业务层实现
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    //注入dao
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Page findEmployeeListPage(Employee searchData, int currentPage, int pageSize) {

        //分页条件对象:参数1：当前的页码，参数2：每页显示的最大记录数
        Page pageRequest =Page.of(currentPage,pageSize);

        //业务的条件对象
        QueryWrapper<Employee> qw =new QueryWrapper<>();


        //一定要判断业务条件是否存在，否则语句会有问题
        //判断是避免空指针异常
        if(searchData!=null){
            //姓名有没有
            if(StringUtils.hasText(searchData.getName())){
                qw.like("name",searchData.getName());
            }
            //员工账号有没有
            if(StringUtils.hasText(searchData.getAccount())){
                qw.eq("account",searchData.getAccount());
            }

            //联系方式有没有
            if(StringUtils.hasText(searchData.getPhone())){
                qw.eq("phone",searchData.getPhone());
            }

            //角色有没有
            if(StringUtils.hasText(searchData.getRole())){
                qw.eq("role",searchData.getRole());
            }
        }

        //参数1：请求的分页条件对象
        //参数2：业务的条件对象
        Page pageResponse = employeeMapper.selectPage(pageRequest, qw);

        return pageResponse;
    }

    @Override
    public void saveEmployee(Employee employee) {
        //对原始密码加密
        employee.setPassword(
                BCrypt.hashpw(employee.getPassword())
        );

        employeeMapper.insert(employee);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateById(employee);
    }

    @Override
    public void deleteEmployeeById(String id) {
        //物理删除
        employeeMapper.deleteById(id);
        //逻辑删除--更新操作--将用户禁用
//        Employee employee=new Employee();
//        employee.setId(id);
//        employee.setStatus("inactive");
//        employeeMapper.updateById(employee);
    }

    @Override
    public Employee findEmpolyeeByAccountAndPassword(String account, String password) {

        //条件
//        QueryWrapper qw=new QueryWrapper();
//        qw.eq("account",account);
//        qw.eq("password",password);
        //代码是不对的。
//        qw.eq("password",BCrypt.hashpw(password));


//        List<Employee> list = employeeMapper.selectList(qw);
//        return list.get(0);
        //如果查询的结果没有，则返回空；如果有多个，则返回第一个
//        return employeeMapper.selectOne(qw);



        //登录逻辑---新
        //1.根据用户名去数据库查询对象
        //条件
        QueryWrapper qw=new QueryWrapper();
        qw.eq("account",account);
        //根据用户名查询出来的数据
        Employee employeeLogin = employeeMapper.selectOne(qw);

        //2.比较密码
        // BCrypt.checkpw(明文，密文);
        boolean checkpw = BCrypt.checkpw(password, employeeLogin.getPassword());
        //判断
        if(!checkpw){
            //如果密码不对
            return null;
            //就想让用户知道密码不正确
//            throw new RuntimeException("密码不正确");
        }

        //登录成功，返回员工对象
        return employeeLogin;

    }
}
